package com.tgu.home_action.controller.admin;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.annotation.MyLog;
import com.tgu.home_action.pojo.exception.IMException;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.admin.AdminCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "管理员的案件管理")
@RestController
@PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
@RequestMapping("/admin/case")
public class AdminCaseController {
    @Autowired
    private AdminCaseService adminCaseService;

    @ApiOperation("查询所有案件信息（可以根据id,familyId,oldManId,state,province,city,district筛选）")
    @GetMapping
    public ResponseData<Page<Case>> getCases(Case caseInformation) {
        return adminCaseService.getCases(caseInformation);
    }

    @ApiOperation("查询案件动态")
    @GetMapping("/dynamic")
    public ResponseData<List<Dynamic>> getCaseDynamic(@RequestParam Integer caseId) {
        return adminCaseService.getCaseDynamic(caseId);
    }

    @MyLog(actionId = 9, role = "family", type = 4)
    @ApiOperation("结束案件（只有管理员可操作，state为2表示已找到，state为4时表示已取消）")
    @PostMapping("/end")
    public ResponseData<Object> cancelCase(@ApiJsonObject(name = "AdminEndCase", value = {
            @ApiJsonProperty(key = "caseId", description = "案件ID(int)"),
            @ApiJsonProperty(key = "state", description = "结束案件状态")
    }) @RequestBody Map<String, Object> param) {
        Integer caseId = (Integer) param.get("caseId");
        int state = (int) param.get("state");
        return adminCaseService.endCase(caseId, (short) state);
    }

    @ApiOperation("管理员加入/退出案件群组")
    @PostMapping("/group")
    public ResponseData<String> joinGroup(@ApiJsonObject(name = "AdminJoinGroup", value = {
            @ApiJsonProperty(key = "caseId", description = "案件ID(int)"),
            @ApiJsonProperty(key = "type", description = "1为加入，2为退出")
    }) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("adminUser") Admin admin) {
        Integer caseId = (Integer) param.get("caseId");
        int type = (int) param.get("type");
        try {
            return adminCaseService.joinGroup(caseId, type, admin);
        } catch (IMException e) {
            return new ResponseData<>(ResponseState.ERROR.getMessage(), ResponseState.ERROR.getValue());
        }
    }

}
